package com.goods.business.service.imp;

import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangliang
 * @Date 2021/11/10 22:02 Administrator
 * @Description
 */
@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;
    @Override
    public  Health  isReport(Long id) {
       //拿到id取数据库查询，与当前时间去数据库查询看是否此用户打卡成功


            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            String format = simpleDateFormat.format(date);
            Health health = healthMapper.likeHealth(format, id);

/*        Example example = new Example(Health.class);
        Example.Criteria criteria = example.createCriteria();
         criteria.andEqualTo("useId", id).andLike("creatTime",format+"%");*/
        return health;
        }


    /**
     * 签到记录
     * @param map
     * @return
     */
    @Override
    public PageVO<HealthVO> history(Map map) {
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        List<Health> healthList = healthMapper.selectAll();
        List<HealthVO> healthVOList = healthList.stream().map(health -> {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health, healthVO);
            return healthVO;
        }).collect(Collectors.toList());
        //总记录数
        int total=healthVOList.size();
        //从每页的最后一页
        int size=(pageNum-1)*pageSize+pageSize;
        List<HealthVO> healthVOList1=healthVOList.subList((pageNum-1)*pageSize,size>total?total:size);
        PageVO<HealthVO> healthVOPageVO = new PageVO<>(total, healthVOList1);
        return healthVOPageVO;
    }

    /**
     * 立即打卡
     * @param healthVO
     */
    @Override
    public void report(HealthVO healthVO) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }
}
